﻿(*                  FnSharp              

   Base class library extensions and frameworks aimed 
   specifically at improving F# developers lives!

   Written by: Huw Simpson
   License: Microsoft Public License (Ms-PL) 
   Available at: http://fnsharp.codeplex.com

*)
namespace FnSharp.Bcl.Core

    open System
    open System.Threading

    // Adapted from http://fssnip.net/6W, ms-pl license (Ademar Gonzalez)

    type var = (unit -> unit) * (unit -> unit)

    /// A Universal type in which any other type can be embedded and retrieved.
    [<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
    module Var = 
        type private Binder<'a> = ('a -> var) * (var -> 'a option)

        let private embed<'a> (): Binder<'a> =
            let r = ref None
            let put x = (fun () -> r := Some x), (fun () -> r := None) 
            let get (f, g) = f (); let res = !r in g (); res in
            put,get

        /// Bind a getter and setter function for embedded type access.
        let bind<'a> : Binder<'a> = embed ()

        